<?php
// 本类由系统自动生成，仅供测试用途
namespace MyClass;
class Xdgg extends \MyClass\Common {
    public function Index(){	
		$this->v();
    }
    //采购中标公告接口
	public function Bid(){
		//采购中标公告列表
		$condition = " 1=1";
		if($_REQUEST['city']) {
			$condition.= " AND ProvinceCity = ".$_REQUEST['city'];
			$this->s('cityid',$_REQUEST['city']);

		}
		if($_REQUEST['cityname']){
				$this->s('cityname',$_REQUEST['cityname']);
			}
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'Pubtime desc';
			} else{
				$ordertype = 'Pubtime asc';
			}
			$this->s("order",$_REQUEST['order']);	
		}else{
			$ordertype = 'Pubtime desc';
		}
		$condition.= " AND Status = 2";
	if(!$_SESSION['UserInfo']['id']){//未登录
		$condition.= " AND Cgtype=1";
		//$count = count($this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->select());
		//$p = new \Extend\Page($count,15);
	    $list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit(6)->select();
		
	
		$this->s('list',$list);
	 }else{
		if(!isset($_SESSION['UserInfo']['cid'])){
			$condition.= " AND Cgtype=1";
			//$count = count($this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->select());
			//$p = new \Extend\Page($count,15);
			$list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit(6)->select();
		
		}else{
				$bus = $this->m("yaojia")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
			
				foreach($bus as $key=>$vo){
					$bgscid[] = $vo['Bid'];
				}
				if($bgscid>0){
					$bgscid = join(",",$bgscid);

				}else{
					$bgscid = 99999999999999;//暂无此数;
				}
				//$count = count($this->m('bid')->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->select());

				$list = $this->m('bid')
					->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")
					->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")
					->order($ordertype)
					->limit(6)
					->select();
		
			}
			$this->s('list',$list);
		}
		
		$this->v();
    }
//采购详情
   public function BideTails(){
		if(!isset($_REQUEST['Id'])){
			$this->error("非法操作");
		}
		$Id = $_REQUEST['Id'];
        $info = $this->m('bid')->find($Id);
		$productsjg = $this->m('bid_order')->where("Bid=".$Id." AND Status = 1")->find();
		$jgproducts =unserialize($productsjg['Product']);
		$jgproducts = $this->object_array($jgproducts);
		$this->s('jgproducts',$jgproducts);
		
		//$products = unserialize($info['Product']);
		//$products = $this->object_array($products);
		if($info['Files']) {
            $array = explode('/',$info['Files'] );
            $file_list = array_filter($array);
        } else {
            $file_list = array();
        }
		
	
		if($_SESSION['UserInfo']['id']){//是否登陆
			if($_SESSION['UserInfo']['cid']){
				$cid = $_SESSION['UserInfo']['cid'];
				$CompanyName = $this->m("company")->field("CompanyName")->where("Id=".$cid)->find();
				$this->s("CompanyName",$CompanyName);
			}
		}
		
		 $order = $this->m('order')->where("Zbid=".$Id)->find();
            if(!$order) {
                $this->error('参数错误！');
          }
		$bid_order = $this->m('bid_order')->where("Bid=".$Id)->find();
		$bid_order['Product'] = unserialize($this->object_array($bid_order['Product']));//序列化转换为数组

	
		$cname = $this->m('company')->where(array('Id='.$order['Seller']))->find();
		$buy = $this->m('bid')->where(array('Status' => 1 ))->order('Pubtime DESC')->limit(10)->select();
		$this->s('buy',$buy);
		$this->s('file_list',$file_list);
		$this->s('cname',$cname['CompanyName']);
		$this->s('companyid',$cname['Id']);
		$this->s('order',$order);
		$this->s('bid_order_Product',$bid_order['Product']);
		$this->s('bid_order_file',$bid_order['XdId']);
        $this->s('info',$info);
      //  $this->s('products',$products);
        $this->v();
    }
	  //产品反序列化后转换成数组
    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
	
	public function PageAjax(){
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
	
		$condition = " 1=1";
		if($_REQUEST['city']) {
			$condition.= " AND ProvinceCity = ".$_REQUEST['city'];
		}
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'Pubtime desc';
			} else{
				$ordertype = 'Pubtime asc';
			}
		}else{
			$ordertype = 'Pubtime desc';
		}
		$condition.= " AND Status = 2";

		if(!$_SESSION['UserInfo']['id']){
			$condition.= " AND Cgtype=1";
			$count = count($this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->select());
			$list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit($thispage.", 6")->select();
		}else{
			if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
		}
			$bus = $this->m("yaojia")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
		
			foreach($bus as $key=>$vo){
				$bgscid[] = $vo['Bid'];
			}
			if($bgscid>0){
				$bgscid = join(",",$bgscid);

			}else{
				$bgscid = 99999999999999;//暂无此数;
			}
			$count = count($this->m('bid')->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->select());

			$list = $this->m('bid')
				->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")
				->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")
				->order($ordertype)
				->limit($thispage.", 6")
				->select();
		}
		foreach($list as $key=>$v){
			
			$list[$key]['Pubtime'] = date("Y-m-d",$v['Pubtime']);
		}
		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}
		echo json_encode($list);
	
	}
	
}